<?php

namespace App\Http\Controllers\Admin;

use App\Models\PrizeRank;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\PrizeLog;
use Excel;

class PrizeLogController extends Controller
{
    public function index(Request $request){
        if($request->ajax()){
            $data = array();
            $data['draw'] = $request->get('draw');
            $start = $request->get('start');
            $length = $request->get('length');
            $order = $request->get('order');
            $columns = $request->get('columns');
            $search = $request->get('search');
            $x_uid = $request->get('x_uid');
            $data['recordsTotal'] = PrizeLog::count();
            if (strlen($search['value']) > 0 || $x_uid) {
                $data['recordsFiltered'] = PrizeLog::where(function ($query) use ($search,$x_uid) {
//                    $query->where('x_uid', 'LIKE', '%' . $search['value'] . '%');
                     $query->orwhere('x_uid', '=', $x_uid);
                })->count();

                $data['data'] = PrizeLog::with('users','PrizeRank','Prize')->where(function ($query) use ($search,$x_uid) {
//                    $query->where('x_uid', 'LIKE', '%' . $search['value'] . '%')
                    $query->orwhere('x_uid', '=', $x_uid);
                })
                    ->skip($start)->take($length)
                    ->orderBy($columns[$order[0]['column']]['data'], $order[0]['dir'])
                    ->get();

            }else{
                $data['recordsFiltered'] = PrizeLog::count();
                $where =array();

                $data['data'] = PrizeLog::with('users','PrizeRank','Prize')->where($where)
                    ->skip($start)->take($length)
                    ->orderBy($columns[$order[0]['column']]['data'], $order[0]['dir'])
                    ->get();

            }


            return response()->json($data);
        }
        return view('admin.prizelog.index');
    }

    public function export(){
        $data = PrizeLog::with(array('users'=>function($query){
            $query->select('username','id','phone');
        },'PrizeRank'=>function($query){
            $query->select('name','id');
        },'Prize'=>function($query){
            $query->select('name','id');
        }))->where('status','!=',-1)->get();
        $cellData[] = array('订单号','姓名','手机号码','奖品等级','奖品名称','奖品状态','获奖时间');
        foreach ($data->toArray() as $row){
            $cellData[]=array(
                "'".$row['order'],
                $row['users']['username'],
                $row['users']['phone'],
                $row['prize_rank']['name'],
                $row['prize']['name'],
                getStatus($row['status']),
                $row['created_at']
            );
        }
        Excel::create('中奖记录',function($excel) use ($cellData){
            $excel->sheet('score', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
    }
}
